<?php

namespace app\common\model\work;

use think\Model;


class Protect extends Model
{

    

    

    // 表名
    protected $name = 'work_warranty';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'billing_data_text',
        'examinetime_text',
        'type_data_text',
        'collect_data_text',
        'warranty_time_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '7' => __('Status 7')];
    }

    public function getBillingDataList()
    {
        return ['0' => __('Billing_data 0'), '1' => __('Billing_data 1')];
    }

    public function getTypeDataList()
    {
        return ['0' => __('Type_data 0'), '1' => __('Type_data 1')];
    }

    public function getCollectDataList()
    {
        return ['0' => __('Collect_data 0'), '1' => __('Collect_data 1')];
    }
    public function getCollectDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['collect_data']) ? $data['collect_data'] : '');
        $list = $this->getCollectDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getOpenDataList()
    {
        return ['0' => __('Open_data 0'), '1' => __('Open_data 1')];
    }
    public function getOpenDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_data']) ? $data['open_data'] : '');
        $list = $this->getOpenDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBillingDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['billing_data']) ? $data['billing_data'] : '');
        $list = $this->getBillingDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExaminetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['examinetime']) ? $data['examinetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTypeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_data']) ? $data['type_data'] : '');
        $list = $this->getTypeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWarrantyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['warranty_time']) ? $data['warranty_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setExaminetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setWarrantyTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function hospital()
    {
        return $this->belongsTo('app\common\model\Hospital', 'hospital_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function engineer()
    {
        return $this->belongsTo('app\common\model\User', 'engineer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function sale()
    {
        return $this->belongsTo('app\common\model\User', 'sale_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function equipment()
    {
        return $this->belongsTo('app\common\model\Equipment', 'equipment_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
